package us.icitap.timsportal.business;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import us.icitap.timsportal.entity.Module;
import us.icitap.timsportal.entity.OrgStructure;
import us.icitap.timsportal.entity.Role;
import us.icitap.timsportal.entity.User;

public class SimpleUser implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = -5675726226377046333L;
	private String username;
	private String email;
	private Set<String> roles = new HashSet<String>();
	private OrgStructure baseStructure;
	private SimpleOfficer officer;
	
	public SimpleUser(){}
	
	public SimpleUser(User user){
		this.username = user.getUsername();
		this.email = user.getEmail();
		this.baseStructure = user.getBaseStructure();
		for(Role r: user.getRoles()){	
			roles.add(r.getId());
		}
		if(user.getOfficer()!=null && user.getOfficer().getOrgStructure()!=null){
			this.officer = new SimpleOfficer(user.getOfficer());
		}
	}
	
	public void retainModuleRoles(Module m){
		Set<String> modRoles = new HashSet<String>();
		for(Role r: m.getRoles())
			modRoles.add(r.getId());
		this.roles.retainAll(modRoles);
	}
	
	public void retainModuleStructure(String[] officerOSList, Module m){
		if(this.baseStructure!=null)
			return;
		loop:
			for(String id: officerOSList){
				for(OrgStructure os: m.getOrgStructures()){
					if(Integer.parseInt(id.trim()) == os.getId()){
						this.baseStructure = os;
						break loop;
					}
				}				
			}
	}
		
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public Set<String> getRoles() {
		return roles;
	}
	public void setRoles(Set<String> roles) {
		this.roles = roles;
	}
	public OrgStructure getBaseStructure() {
		return baseStructure;
	}
	public void setBaseStructure(OrgStructure baseStructure) {
		this.baseStructure = baseStructure;
	}
	public SimpleOfficer getOfficer() {
		return officer;
	}
	public void setOfficer(SimpleOfficer officer) {
		this.officer = officer;
	}

	@Override
	public String toString() {
		return "SimpleUser [username=" + username + ", email=" + email
				+ ", roles=" + roles + ", baseStructure=" + baseStructure
				+ ", officer=" + officer + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((baseStructure == null) ? 0 : baseStructure.hashCode());
		result = prime * result
				+ ((username == null) ? 0 : username.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof SimpleUser))
			return false;
		SimpleUser other = (SimpleUser) obj;
		if (baseStructure == null) {
			if (other.baseStructure != null)
				return false;
		} else if (!baseStructure.equals(other.baseStructure))
			return false;
		if (username == null) {
			if (other.username != null)
				return false;
		} else if (!username.equals(other.username))
			return false;
		return true;
	}
	
}
